package com.corpgovernment.common.supplier;

import com.ctrip.corp.obt.generic.core.http.HttpResponse;
import com.ctrip.corp.obt.generic.utils.JsonUtils;

/**
 * 将请求分销的结果转换成统一错误信息的转换器抽象类
 *
 * @author xukang zhang
 * @date 2024/2/7 19:06
 */
public abstract class ResponseConverter<T> {
    /**
     * 数据类型的CLass对象
     */
    private final Class<T> clazz;

    protected ResponseConverter(Class<T> clazz) {
        this.clazz = clazz;
    }

    public Class<T> getClazz() {
        return clazz;
    }

    /**
     * 将供应商的响应转换成统一的HttpResponse
     *
     * @param json
     * @return
     */
    public HttpResponse<T> convertSupplierResponse(String json) {
        T response = JsonUtils.parse(json, clazz);
        if (response == null) {
            return null;
        }
        int resultCode = convertResultCode(response);
        String message = convertMessage(response);
        return new HttpResponse<>(resultCode, message, response);
    }


    /**
     * 从响应体对象获取resultCode
     *
     * @param response
     * @return
     */
    protected abstract int convertResultCode(T response);


    /**
     * 从响应体对象获取message
     *
     * @param response
     * @return
     */
    protected abstract String convertMessage(T response);


}
